﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using Bewise.SmartIndexManager.Core;
using Bewise.SmartIndexManager.Core.Classes.Database;
using Bewise.SmartIndexManager.Core.Classes.Instance;
using Bewise.SmartIndexManager.Core.Classes.Options;
using Bewise.SmartIndexManager.Core.Classes.Types;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Runtime.Design;

namespace Bewise.SmartIndexManager
{
    public static class Tools
    {
        public static string Serialize(TaskInfosContainer obj)
        {
            Type[] extraTypes = {
                                    typeof (RequiredMinimumDatabaseOption), typeof (RequiredMinimumIndexOption),
                                    typeof (SeuilIndexOption), typeof (InstanceInfos), typeof (DatabaseInfos)
                                };

            return SerializeObject(obj, extraTypes);
        }
        public static TaskInfosContainer Deserialize(string xmlbody)
        {
            Type[] extraTypes = {
                                    typeof (RequiredMinimumDatabaseOption), typeof (RequiredMinimumIndexOption),
                                    typeof (SeuilIndexOption), typeof (InstanceInfos), typeof (DatabaseInfos)
                                };
            return DeserializeObject(xmlbody, extraTypes);

        }
        private static string SerializeObject(TaskInfosContainer obj, Type[] extraTypes)
        {
            string XmlString = String.Empty;

            try
            {
                var memStream = new MemoryStream();
                var serializer = new XmlSerializer(obj.GetType(), extraTypes);
                var XmlText = new XmlTextWriter(memStream, Encoding.Default);
                serializer.Serialize(XmlText, obj);
                XmlString = Encoding.Default.GetString(memStream.ToArray());
                memStream.Close();
            }

            catch
            {
                throw;
            }

            return XmlString;
        }
        private static TaskInfosContainer DeserializeObject(string xmlbody, Type[] extraTypes)
        {
            byte[] buffer = Encoding.Default.GetBytes(xmlbody);
            var input = new MemoryStream(buffer);
            var xMlWriter = new XmlTextWriter(input, Encoding.Default);
            var serialzier = new XmlSerializer(typeof(TaskInfosContainer), extraTypes);
            return (TaskInfosContainer)serialzier.Deserialize(input);
        }

        public static List<Microsoft.SqlServer.Management.Smo.Database> GetListDatabases(TaskInfosContainer taskInfosContainer, Microsoft.SqlServer.Management.Smo.Server server)
        {
            var listDatabases = new List<Microsoft.SqlServer.Management.Smo.Database>();

            switch (taskInfosContainer.SelectedDatabases)
            {
                case DatabasesTypeEnum.AllDatabase:
                    listDatabases.AddRange(server.Databases.Cast<Microsoft.SqlServer.Management.Smo.Database>());
                    break;
                case DatabasesTypeEnum.TheseDatabases:

                    foreach (var selectedDatabaseInfos in taskInfosContainer.InstanceInfos.Databases)
                    {
                        if (selectedDatabaseInfos != null)
                            try
                            {
                                var database = server.Databases[selectedDatabaseInfos.DatabaseName];
                                if (database != null)
                                    listDatabases.Add(database);
                            }
                            catch (Exception)
                            {
                                // non bloquant
                            }
                    }
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
            return listDatabases;
        }

        public static List<string> GetListADO_NETProviders(IDtsConnectionService connections)
        {
            //return connections.GetConnectionsOfType("ADO.NET").Cast<string>().Select(x => x).ToList();
            var listAdoNetProviders = new List<string>();

            ConnectionEnumerator connectionEnumerator = connections.GetConnections().GetEnumerator();
            while (connectionEnumerator.MoveNext())
            {
                Microsoft.SqlServer.Dts.Runtime.ConnectionManager current = connectionEnumerator.Current;
                // lecture du code dans Reflector dans la classe 
                if (current.CreationName.ToUpper().Contains("ADO.NET"))
                    listAdoNetProviders.Add(current.Name);
            }
            return listAdoNetProviders;
        }

public static ConnectionManager GetConnectioManager(Connections connections, string cmName)
{
    ConnectionEnumerator connectionEnumerator = connections.GetEnumerator();
    while (connectionEnumerator.MoveNext())
    {
        ConnectionManager current = connectionEnumerator.Current;
        // lecture du code dans Reflector.
        if (current.Name == cmName)
            return current;
    }
    return null;
}
    }
}
